<template>
  <div>
    <van-row>
      <!-- 左上角logo -->
      <router-link to="/home">
        <van-col class="girl" span="4">
          <img src="../../assets/xiaonvhai.png" alt />
        </van-col>
      </router-link>
      <!-- 搜索框 -->
      <van-col span="16">
        <form action="/">
          <van-search
            ref="search"
            v-model="value"
            shape="round"
            background="white"
            placeholder="请输入搜索关键词"
            @search="onSearch"
          />
        </form>
      </van-col>
      <!-- 发布 -->
      <router-link to="/issue">
        <van-col class="issue" span="4">
          <img src="../../assets/fabu_caise.png" alt />
        </van-col>
      </router-link>
    </van-row>
    <!-- 搜索到的内容 -->
    <div class="card" v-for="(item,index) in searchList" :key="index">
      <div>
        <router-link :to="{path:'/cardinfo/'+item.postsId}">
          <p class="card-title">{{item.title}}</p>
          <p class="card-intro">{{item.intro}}</p>
        </router-link>
        <div class="author-intro">
          <img class="avatar" :src="item.avatar" alt />
          <span class="author">{{item.userName}}</span>
        </div>
      </div>
    </div>
  <!-- 历史记录 -->
   <div>
      <div class="history-list">
        <span class="my-history">历史记录</span>
        <van-icon name="delete" @click="clearHistory" size="20"/>
      </div>
      <!-- 历史记录内容 -->
      <span  v-for="item in conlist" :key="item.index" class="delete-tag">
        <van-tag color="#ffe1e1" text-color="#ad0000">
          {{item.post}}&nbsp;&nbsp;
          <van-icon name="delete" size="18" @click="del(index,item.postsId)"/>
        </van-tag>
      </span>
   </div>
  </div>
</template>
<script>
import { Toast } from "vant";
import { postList } from "../../api/forum-api.js";
export default {
  data() {
    return {
      value: "",
      active: "1",
      searchList: [],
      conlist:[],
      title:"",
      postsId:"",
      index:"",
    };
  },
  created() {
    this.conlist = JSON.parse(localStorage.getItem("history")||"[]")
  },
  methods: {
    //搜索接口
    onSearch(value) {
      postList(this.value).then(res => {
        console.log(res.rows);
        this.searchList = res.rows;
        //console.log(this.searchList[0].postsId);
      });

      //2、把新搜索的记录存放到conlist数组中
      this.conlist.push({id:Date.now(),post:this.value})
      //3、将新纪录同步到localStorage中
      localStorage.setItem('history',JSON.stringify(this.conlist));
      //localStorage.clear();
    },
    //清除全部历史记录
    clearHistory(){
      localStorage.clear()
      this.conlist='[]'
    },
    //单个删除
    del(index){
      this.conlist.splice(index, 1)
      localStorage.setItem('history',JSON.stringify(this.conlist));
    }

  }
};
</script>
<style lang="less" scoped>
.girl {
  padding: 10px;
}
.issue {
  padding: 10px;
}
//卡片的样式
*{
  margin: 0;
}
.box {
  margin-top: 20px;
  margin-bottom: 50px;
  margin-left: 5%;
}
.card {
  width: 90%;
  height: 80px;
  border: 1px solid gray;
  border-radius: 5px;
  padding: 5px;
  position: relative;
  margin: 10px auto;
  .myHeart {
    position: absolute;
    bottom: 10px;
  }
  .author-intro {
    width: 30%;
    position: absolute;
    right: 0;
    bottom: 5px;
    .avatar {
      width: 20px;
      height: 20px;
      border-radius: 50%;
      margin-top: 5px;
    }
    .author {
      font-size: 12px;
    }
  }
  .card-title {
    font-size: 14px;
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
    font-weight: bold;
    color: black;
  }
  .card-intro {
    margin-top: 10px;
    font-size: 12px;
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
    color: gray;
  }
}
.history-list {
  display: flex;
  justify-content: space-around;
  margin: 10px 0;
}
.my-history {
  font-weight: 700;
  font-size: 14px;
}
.delete-tag span {
  margin:10px 10px 5px;
}
.van-tag--medium {
    font-size: 14px;
    margin-top: 6px;
}

</style>